﻿#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QSqlTableModel* model,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    m_whModel = model;

    init();
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::init()
{
    this->setFixedSize(this->size());
    updateType();
    updateVender();
    updateProName();
}

void Dialog::updateType()
{
    ui->combType->clear();
    QStringList types;
    types.append("所有类型");
    for(int i = 0;i < m_whModel->rowCount();++i){
        if(!types.contains(m_whModel->data(m_whModel->index(i,2)).toString())){
            types.append(m_whModel->data(m_whModel->index(i,2)).toString());
        }
    }
    ui->combType->addItems(types);
}

void Dialog::updateVender()
{
    ui->combVender->clear();
    QStringList venders;
    venders.append("所有厂家");
    if(ui->combType->currentText() == "所有类型"){
        for(int i = 0;i < m_whModel->rowCount();++i){
            if(!venders.contains(m_whModel->data(m_whModel->index(i,3)).toString())){
                venders.append(m_whModel->data(m_whModel->index(i,3)).toString());
            }
        }
    }

    else{
        for(int i = 0;i < m_whModel->rowCount();++i){
            if(!venders.contains(m_whModel->data(m_whModel->index(i,3)).toString()) && ui->combType->currentText()==m_whModel->data(m_whModel->index(i,2)).toString()){
                venders.append(m_whModel->data(m_whModel->index(i,3)).toString());
            }
        }
    }
    ui->combVender->addItems(venders);
}

void Dialog::updateProName()
{

    ui->combName->clear();
    QStringList names;
    if(ui->combType->currentText() == "所有类型" && ui->combVender->currentText() == "所有厂家"){
        for(int i = 0;i < m_whModel->rowCount();++i){

            names.append(m_whModel->data(m_whModel->index(i,1)).toString());

        }
    }else if(ui->combType->currentText() == "所有类型" && ui->combVender->currentText() != "所有厂家"){
        for(int i = 0; i < m_whModel->rowCount();++i){
            if(m_whModel->data(m_whModel->index(i,3)).toString() == ui->combVender->currentText()){
                names.append(m_whModel->data(m_whModel->index(i,1)).toString());
            }
        }
    }else if(ui->combType->currentText() != "所有类型" && ui->combVender->currentText() == "所有厂家")
    for(int i = 0;i < m_whModel->rowCount();++i){
        if(m_whModel->data(m_whModel->index(i,2)).toString() == ui->combType->currentText()){
            names.append(m_whModel->data(m_whModel->index(i,1)).toString());
        }
    }
    else if(ui->combType->currentText() != "所有类型" && ui->combVender->currentText() != "所有厂家"){
        for(int i = 0;i < m_whModel->rowCount();++i){
        if(m_whModel->data(m_whModel->index(i,2)).toString() == ui->combType->currentText() && m_whModel->data(m_whModel->index(i,3)).toString() == ui->combVender->currentText()){
            names.append(m_whModel->data(m_whModel->index(i,1)).toString());
        }
        }
    }

    ui->combName->addItems(names);
}

void Dialog::on_combType_currentIndexChanged(int index)
{
    Q_UNUSED(index);
    updateVender();
    updateProName();
}


void Dialog::on_combVender_currentIndexChanged(int index)
{
    Q_UNUSED(index);
    updateProName();
}


void Dialog::on_combName_currentIndexChanged(int index)
{
    Q_UNUSED(index);
    if(ui->combType->currentText() == "所有类型" && ui->combVender->currentText() == "所有厂家"){
        for(int i = 0;i < m_whModel->rowCount();++i){
            if(m_whModel->data(m_whModel->index(i,1)).toString() == ui->combName->currentText()){
                ui->labPrice->setText(m_whModel->data(m_whModel->index(i,4)).toString());
                ui->spinCount->setMaximum(m_whModel->data(m_whModel->index(i,5)).toInt());
                break;
            }
        }
    }else if(ui->combType->currentText() == "所有类型" && ui->combVender->currentText() != "所有厂家"){
        for(int i = 0;i < m_whModel->rowCount();++i){
            if(m_whModel->data(m_whModel->index(i,1)).toString() == ui->combName->currentText() && m_whModel->data(m_whModel->index(i,3)).toString() == ui->combVender->currentText()){
                ui->labPrice->setText(m_whModel->data(m_whModel->index(i,4)).toString());
                ui->spinCount->setMaximum(m_whModel->data(m_whModel->index(i,5)).toInt());
                break;
            }
        }
    }
    else if(ui->combType->currentText() != "所有类型" && ui->combVender->currentText() == "所有厂家"){
        for(int i = 0;i < m_whModel->rowCount();++i){
            if(m_whModel->data(m_whModel->index(i,1)).toString() == ui->combName->currentText() && m_whModel->data(m_whModel->index(i,2)).toString() == ui->combType->currentText()){
                ui->labPrice->setText(m_whModel->data(m_whModel->index(i,4)).toString());
                ui->spinCount->setMaximum(m_whModel->data(m_whModel->index(i,5)).toInt());
                break;
            }
        }
    }
    else if(ui->combType->currentText() != "所有类型" && ui->combVender->currentText() != "所有厂家"){
        for(int i = 0;i < m_whModel->rowCount();++i){
            if(m_whModel->data(m_whModel->index(i,1)).toString() == ui->combName->currentText() && m_whModel->data(m_whModel->index(i,2)).toString() == ui->combType->currentText() && m_whModel->data(m_whModel->index(i,3)).toString() == ui->combVender->currentText()){
                ui->labPrice->setText(m_whModel->data(m_whModel->index(i,4)).toString());
                ui->spinCount->setMaximum(m_whModel->data(m_whModel->index(i,5)).toInt());
                break;
            }
        }
    }
}


void Dialog::on_btnCancel_clicked()
{
    this->reject();
}


void Dialog::on_btnOk_clicked()
{
    if(ui->combType->currentText() == "所有类型" && ui->combVender->currentText() == "所有厂家"){
        for(int i = 0;i < m_whModel->rowCount();++i){
            if(m_whModel->data(m_whModel->index(i,1)).toString() == ui->combName->currentText()){
                m_whModel->setData(m_whModel->index(i,5),m_whModel->data(m_whModel->index(i,5)).toInt() - ui->spinCount->value());
                break;
            }
        }
    }else if(ui->combType->currentText() == "所有类型" && ui->combVender->currentText() != "所有厂家"){
        for(int i = 0;i < m_whModel->rowCount();++i){
            if(m_whModel->data(m_whModel->index(i,1)).toString() == ui->combName->currentText() && m_whModel->data(m_whModel->index(i,3)).toString() == ui->combVender->currentText()){
                m_whModel->setData(m_whModel->index(i,5),m_whModel->data(m_whModel->index(i,5)).toInt() - ui->spinCount->value());

                break;
            }
        }
    }
    else if(ui->combType->currentText() != "所有类型" && ui->combVender->currentText() == "所有厂家"){
        for(int i = 0;i < m_whModel->rowCount();++i){
            if(m_whModel->data(m_whModel->index(i,1)).toString() == ui->combName->currentText() && m_whModel->data(m_whModel->index(i,2)).toString() == ui->combType->currentText()){
                m_whModel->setData(m_whModel->index(i,5),m_whModel->data(m_whModel->index(i,5)).toInt() - ui->spinCount->value());

                break;
            }
        }
    }
    else if(ui->combType->currentText() != "所有类型" && ui->combVender->currentText() != "所有厂家"){
        for(int i = 0;i < m_whModel->rowCount();++i){
            if(m_whModel->data(m_whModel->index(i,1)).toString() == ui->combName->currentText() && m_whModel->data(m_whModel->index(i,2)).toString() == ui->combType->currentText() && m_whModel->data(m_whModel->index(i,3)).toString() == ui->combVender->currentText()){
                m_whModel->setData(m_whModel->index(i,5),m_whModel->data(m_whModel->index(i,5)).toInt() - ui->spinCount->value());

                break;
            }
        }
    }

    if(m_whModel->isDirty()){
        if(m_whModel->submitAll()){
            QMessageBox::information(0,"提示","出售成功!");
        }
    }

    this->accept();
}

